/**
 * Created by Administrator on 2017/6/30 0030.
 */
"use strict";

angular.module('app.sensor').controller('PerLogCtrl', function ($scope, $http,$cookieStore) {
    $scope.fid = $cookieStore.get('userinfo').fid;
    $scope.purview = false;
    if($scope.fid !== "0" && $scope.fid !== "1"){
        $scope.purview = true;
    }
    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 50, 1000],
        timefrom:'',
        timeto:'',
        statue:'',
        onChange: function () {
        }
    };
    $scope.addobj = {};
    $http.get(baseurl+'/select_idname/project').then(function(res){
        if(res.data.ret == 1){

            if(res.data.info.length > 0){
                $scope.projects = res.data.info;
                for( var i = 0; i < res.data.info.length; i++){
                    $scope.projects[i].id = JSON.stringify($scope.projects[i].id )
                }
                $scope.addobj.projectid = $scope.projects[0].id;
            }

        }
    });
    $scope.sends = function () {
        var page_obj = {
            page:$scope.paginationConf.currentPage,
            rows:$scope.paginationConf.itemsPerPage     
        };
        var tid = $scope.paginationConf.timefrom;
        var toid = $scope.paginationConf.timeto;
        var statue = $scope.paginationConf.statue;
        var query = '?statue=='+statue+'&time='+tid+','+toid;
        $http.post(baseurl+'/list/perworklog'+query,page_obj).then(function(res){
            // console.log(res.data);
            if (res.data.ret = 1) {
                $scope.datas = res.data.rows;
                for( var i = 0; i < $scope.datas.length; i++){
                    if($scope.datas[i].statue == "agree"){
                        $scope.datas[i].statue = "通过";
                        $scope.datas[i].col = "green"
                    }else if($scope.datas[i].statue == -1){
                        $scope.datas[i].statue = "未处理";
                        $scope.datas[i].col = "#BD7B2C"
                    }else{
                        $scope.datas[i].statue = "未通过";
                        $scope.datas[i].col = "#DE0E07"
                    }
                    var obj = $scope.datas[i];
                    var time = obj.time;
                    obj.time = moment(time).format("YYYY-MM-DD HH:mm:ss");
                    if (obj.time == 'Invalid date') {
                        obj.time = time;
                    }

                }
                
                $scope.paginationConf.totalItems = res.data.records;

            }

        })
    };
    
    $scope.exportData = function () {

        var blob = new Blob(["\ufeff" + document.getElementById('exportable').innerHTML], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
        });

        saveAs(blob, "Report.xls");
    };
    // 添加日志
    $scope.show_addbox = function(){
        $scope.addobj = {};
        console.log($scope.addobj)
    };
    $scope.sure_add = function(){
        // console.log($scope.addobj);
        if ( $scope.addobj.projectid == null || $scope.addobj.projectid == '') {
            new Notify('fail','请选择项目');
            return;
        }
        if ( $scope.addobj.title == null || $scope.addobj.title == '' || !$scope.addobj.title) {
            new Notify('fail','请填写标题');
            return;
        }
        if ( $scope.addobj.content == null || $scope.addobj.content == '' || !$scope.addobj.content) {
            new Notify('fail','请填写内容');
            return;
        }

        $http.post(baseurl+'/data/logwork',$scope.addobj).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                 new Notify('success','添加成功');
                 $("#addAlert").modal("hide");
                 $scope.sends();
            }else if (res.data.ret != -4){
                 new Notify('fail','发送失败');
            }
        });
    };
    // 修改

    // 删除
    $scope.showDelDialog = function (id) {
        $scope.delid= id;
        $('#del_box').modal('show');
    };
    $scope.sure_del = function () { 
            $http.delete(baseurl+'/data/logwork?id=='+$scope.delid).then(function(res){
                // console.log(res.data);
                if (res.data.ret == 1) {
                    new Notify('success','删除成功');
                     $('#del_box').modal('hide');
                     $scope.sends();
                }else if (res.data.ret != -4){
                     new Notify('fail','发送失败');
                }
            });
    };
    // 处理日志
    $scope.show_chuli = function(id){
        $scope.chuli_id = id;
        $("#chuli_box").modal('show');
    };
    $scope.sure_agree = function(){
        $http.put(baseurl+'/data/logwork?id=='+$scope.chuli_id,{statue:'agree'}).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                 new Notify('success','修改成功');
                 $("#chuli_box").modal("hide");
                 $scope.sends();
            }else if (res.data.ret != -4){
                 new Notify('fail','发送失败');
            }
        });
    };
    $scope.sure_disagree = function(){
        $http.put(baseurl+'/data/logwork?id=='+$scope.chuli_id,{statue:'disagree'}).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                 new Notify('success','修改成功');
                 $("#chuli_box").modal("hide");
                 $scope.sends();
            }else if (res.data.ret != -4){
                 new Notify('fail','发送失败');
            }
        });
    };
    // 单个图片上传
    $("#file-photo").fileinput({
        language: 'zh',
        uploadUrl: fileurls+'/photos/upload_shop', // you must set a valid URL here else you will get an error
        allowedFileExtensions: ["jpg","png","jpeg"],
        overwriteInitial: false,
        maxFileSize: 10000,
        maxFilesNum: 1,
        showPreview: true
    });
    $("#file-photo").on("fileuploaded", function (event, data, previewId, index) {
        if (data.response.state == 200) {
            $scope.addobj.file_url = data.response.result;
            new Notify('success','图片添加成功');
        }
    });
    // 图片查看器
    $scope.show_image = function(url){
        $("#image_box").modal("show");
        $scope.show_imageurl = url;
    };
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage',$scope.sends);

});

